﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web.Caching;
using System.IO;
using System.Web;

namespace WisPortal
{
    public class XmlHelper
    {
        /// <summary>
        /// GetConfigrationFrom
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static XmlDocument GetConfigration(string key)
        {
            //Read from id format
            return WP.Configration.GetConfigration(key);
        }


        /// <summary>
        /// Return a formatted attribute string when the node's attribute is set and not empty
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributeName"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string InsertAttributeWhenEmpty(XmlNode node, string attributeName, string format)
        {
            string v = GetAttributeValue(node, attributeName);
            if (String.IsNullOrEmpty(v))
                return "";
            else
                return string.Format(format,v);
        }

        public static int GetAttributeInt(XmlNode node, string attributeName)
        {
            return Convert.ToInt32(GetAttributeValue(node, attributeName, "0"));
        }

        public static string GetAttributeValue(XmlNode node, string attributeName)
        {
            return GetAttributeValue(node, attributeName, "");
        }

        public static int GetAttributeInt(XmlNode node, string attributeName, string defaultValue)
        {
            return Convert.ToInt32(GetAttributeValue(node,attributeName,defaultValue));
        }
        /// <summary>
        /// Get the value of a attribute
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributeName"></param>
        /// <param name="defaultValue">if there's no attribute in this node then return the default value</param>
        /// <returns></returns>
        public static string GetAttributeValue(XmlNode node, string attributeName, string defaultValue)
        {
            if (node != null && node.Attributes[attributeName] != null)
                return node.Attributes[attributeName].Value;
            else
                return defaultValue;
        }

        public static string GetNodeValue(XmlNode node)
        {
            return GetNodeValue(node,"");
        }
        
        public static string GetNodeValue(XmlNode node, string defaultValue)
        {
            if (node != null)
                return node.InnerText;
            else
                return defaultValue;
        }
    }
}
